<template>
  <div>
    <el-card shadow="never" class="mb20" style="overflow: inherit">
      <article-form @search="handleSearch" />
    </el-card>
    <el-card shadow="never">
      <!-- <el-button type="primary" icon="el-plus-icon" size="small" @click="handleEdit" /> -->
      <el-button
        class="mb20 fr"
        size="small"
        icon="el-icon-plus"
        type="primary"
        @click="handleEdit(false, {})"
      />
      <e-table
        @getList="handleGetList"
        :tableData="tableData"
        :columns="columns"
        :loading="loading"
        :pageObj="null"
      >
        <el-table-column label="置顶" slot="upFlag" align="center">
          <template slot-scope="scope">{{ scope.row.upFlag === 1 ? '是' : '否' }}</template>
        </el-table-column>
        <el-table-column label="文章标识" slot="articleFlag" align="center">
          <template
            slot-scope="scope"
          >{{ scope.row.articleFlag === 1 ? '头条' : ( scope.row.articleFlag === 2 ? '热门' : '推荐' ) }}</template>
        </el-table-column>
        <el-table-column slot="action" fixed="right" width="50" align="center">
          <template slot-scope="scope">
            <el-dropdown>
              <a class="el-dropdown-link">
                <i class="el-icon-menu"></i>
              </a>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleEdit(true, scope.row)">
                  <a>编辑</a>
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleDelete(scope.row)">
                  <a class="red">删除</a>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </e-table>
      <!-- <article-modal @refresh="handleRefresh" ref="articleModal" /> -->
    </el-card>
  </div>
</template>

<script>
// import ArticleModal from './ArticleModal'
import ArticleForm from './ArticleForm'
import { getCmsArticle, deleteArticle } from '@/api/system'
import { openNotification } from '@/utils/util'

export default {
  components: {
    ArticleForm
    // ArticleModal
  },
  data() {
    return {
      tableData: [],
      columns: [
        {
          label: '文章名称',
          prop: 'articleName'
        },
        {
          label: '栏目名称',
          prop: 'categoryName'
        },
        // {
        //   label: '内容',
        //   prop: 'content',
        //   tooltip: true
        // },
        {
          label: '内容简介',
          prop: 'contentBrief'
        },
        {
          label: '发布者',
          prop: 'issuer'
        },
        {
          slot: 'upFlag'
        },
        {
          slot: 'articleFlag'
        },
        {
          slot: 'action'
        }
      ],
      loading: false,
      queryParam: {
        pageSize: 10,
        pageNo: 1
      }
    }
  },
  methods: {
    handleSearch(param) {
      this.handleGetList(param)
    },
    handleEdit(bool, record) {
      console.log('edit', bool, record)

      // this.$refs.articleModal.edit(bool, record)
      this.$router.push({
        path: '/system/cms-articleDetail',
        query: { bool, record: JSON.stringify(record) }
      })
    },
    // getCmsArticle
    async handleGetList(param = {}) {
      this.loading = true
      this.queryParam = Object.assign(this.queryParam, param)
      const res = await getCmsArticle(this.queryParam)
      if (res.data && res.data.length > 0) {
        const { state, data } = res
        this.tableData = data
        // this.total = total
      } else {
        this.tableData = []
        // this.total = 1
      }
      this.loading = false
    },
    // 删除
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.articleName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await deleteArticle(row.articleId, {})
          openNotification('success', '删除成功')
          _this.handleGetList()
        }
      })
    },
    handleRefresh() {
      this.handleGetList()
    }
  },
  mounted() {
    this.handleGetList()
  }
}
</script>
